package hms.ReservationsModule;

import java.util.*;
import java.util.regex.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.DefaultListModel;
import javax.swing.border.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import hms.generic.*;
import hms.master.MasterView;
import hms.permissions.Permission;
import hms.UserAccountsModule.UserAccount;
import hms.database.*;

/*This class initializes the GUI of the Reservation Module (RVSM) of the Hotel Management System(HMS).
 * Each element will be used in the following sub-modules:
 * -Guest Information
 * -Reservation
 * -Rewards
 * 
 * @author Edgar Onukwugha*/

public class ReservationModule extends AbstractGlobalView{
	
	//components used in RVSM GUI
	/*"protected" to ensure use though RSVM and related sub-modules only*/
	protected JPanel guestSelection, subModule;
	protected JTextField searchBar;
	protected JList selectionMenu;
	protected JScrollPane scrollPane;
	protected DefaultListModel listModel;
	
	
	//initializes other elements used through class
	protected static final Insets insets = new Insets(0,0,0,0);
	
//	CheckInView civ = new CheckInView();
//	GuestInfoView giv = new GuestInfoView();
//	RewardsView rv = new RewardsView();
//	MasterView mv = new MasterView();
	DAO db = DAOnoGUI.getDAO();
	
	//guestNames vector will act as in-between for pulling and putting 
	// names into DB
	Vector <String> guestNames;
	
	//for overall panel
	GridBagConstraints gbc;
	
	//creates view of ReservationModule via MasterView methods
	public ReservationModule(JPanel i_subModule){

		subModule = i_subModule;
		
		// Create and populate panel
		JPanel panel = new JPanel();
		addComponentsToView(panel);
		
		/*Commands to put panel in Master GUI*/
		//will update view status of Master GUI
		super.destroy();
		super.update(panel);
		//will display GUI in Master GUI
		super.display();	
	}
	
	//sets up initial JComponents used in AbstractGlobalView overview
	/*TODO: set up preconditions, postconditions*/
	public void addComponentsToView(Container pane){
		
		//declares all protected components
		guestSelection = new JPanel();
		searchBar = new JTextField();
		
		//setting default list model and other attributes
		listModel = new DefaultListModel();
		selectionMenu = new JList(listModel);
		selectionMenu.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		//selectionMenu.addListSelectionListener( (ListSelectionListener)this );
		selectionMenu.setLayoutOrientation(JList.VERTICAL);	
		
		searchBar.setPreferredSize(new Dimension(200,30));		
		
		//adds components to guestSelection panel
		guestSelection.setBorder(new TitledBorder("Guest Selection"));
		
		//add Action Listeners to components here 
		searchBar.addActionListener(this);
		
		//adding components to GuestSelection panel
		addComponent(guestSelection, searchBar, 0, 2, 2, 1,
				GridBagConstraints.NORTH, GridBagConstraints.BOTH);
		addComponent(guestSelection, selectionMenu, 0, 4, 2, 2,
				GridBagConstraints.SOUTH, GridBagConstraints.BOTH);
		
		guestSelection.add(searchBar);
		guestSelection.add(selectionMenu);
		
		//adds components to subModule panel
		setSubModule(subModule);
					
		//adds components to overall pane		
		addComponent(pane, guestSelection, 0, 0, 20, 10,
			GridBagConstraints.EAST, GridBagConstraints.BOTH);
		addComponent(pane, subModule, 3, 0, 20, 10,
			GridBagConstraints.WEST, GridBagConstraints.BOTH);
	}
	
	//sets overall GUI for sub-modules in RSVM
	/*will be overridden in sub-modules to accommodate separate functionality
	 * in sub-modules*/
	public void setSubModule(JPanel subModule){
		//subModule = new JPanel();
		scrollPane = new JScrollPane(subModule);
		scrollPane.setViewportView(subModule);
		subModule.setBorder(new TitledBorder(""));
	}
	
	//sets location, details of component for a container
	public void addComponent(Container container, Component component,
			int gridx, int gridy, int gridwidth, int gridheight, int anchor,
			int fill) {
				GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
						gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
				container.add(component, gbc);
	}
	
	/*sets queries for searching for guest
	 * includes functionality for searchBar and selectionMenu*/
	/*TODO: set up preconditions, postconditions*/
	public void searchForGuest(){
		/*TODO: set up AutoComplete functionality with guestSelection*/
		
		//pattern for full name
		Pattern firstNamePattern = Pattern.compile("\\w+\\s");
		Pattern lastNamePattern = Pattern.compile("\\s\\w+\\s");
		
		String nameResult = searchBar.getText();
		
		//String firstNameResult = firstNamePattern(nameResult);
		//String lastNameREsult = lastNamePattern(nameResult);
		//SELECT queries
		String selectGuestName = "SELECT CONCAT(FIRST_NAME ' 'LAST_NAME) AS FULL_NAME FROM GUEST" +
				"WHERE FIRST_NAME = AND LAST_NAME = " + nameResult + " =  ";
		
		/*database interactions*/
		//initiate queries
		db.updateCommand(selectGuestName);
		
	}
	
	private void syncListModel() {
		listModel.clear();
		//listModel.ensureCapacity(selectionMenu.size());
		//for(UserAccount ua : selectionMenu) {
		///	if(!ua.isMarkedForDeletion())
		//	listModel.addElement(ua);
		//}
	}
	
	//sets default layout of sub-module section	
	/*sets all action events for JComponents used in RSVM*/
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == searchBar){
			searchForGuest();
		}
		
		if(e.getSource() == selectionMenu){
			//
			syncListModel();
		}
		
	}
	
	public void valueChanged(ListSelectionEvent arg0) {
			if(arg0.getValueIsAdjusting() == false) {
				int index = selectionMenu.getSelectedIndex();
				if( index == -1 ) {
					
				} else {
					
				}
			}
		}
}

